package com.ydgk.controller;

import com.ydgk.bean.Admin;
import com.ydgk.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;


@RequestMapping("/login")
@Controller
public class LoginController {
    @Autowired
    private LoginService loginService;

    @RequestMapping("/toLogin")
    public String toLogin(Admin admin, @RequestParam(value = "cookie" , required = false, defaultValue = "") String cookie, HttpServletResponse response, HttpSession session, Map map){
        Boolean isExsit = loginService.checkExsit(admin);
        if(isExsit){
            Admin admin1 = loginService.checkRight(admin);
            if(admin1 != null){
                if(!cookie .equals("")){
                    Cookie cookieName = new Cookie("aname",admin1.getAname());
                    Cookie cookiePass = new Cookie("apass",admin1.getApass());
                    cookieName.setMaxAge(60 * 60 * 24 * 15);
                    cookiePass.setMaxAge(60 * 60 * 24 * 15);
                    cookieName.setPath("/");
                    cookiePass.setPath("/");
                    response.addCookie(cookieName);
                    response.addCookie(cookiePass);
                }
                session.setAttribute("admin",admin1);
                return "redirect:/toManager";
            }else{
                map.put("msg","密码错误!");
                return "Manager/login";
            }

        }else{
            map.put("msg","此账号不存在!");
            return "Manager/login";
        }

    }

    @RequestMapping(value = "/toCookieLogin")
    public String toCookieLogin(Admin admin, HttpSession session, HttpServletResponse response, Map map){
        Admin admin1 = loginService.checkRight(admin);
        if(admin1 != null){
            session.setAttribute("admin",admin1);
            return "redirect:/toManager";
        }else {
            Cookie cookieName = new Cookie("aname", admin.getAname());
            Cookie cookiePass = new Cookie("apass", admin.getApass());
            cookieName.setMaxAge(0);
            cookiePass.setMaxAge(0);
            cookieName.setPath("/");
            cookiePass.setPath("/");
            response.addCookie(cookieName);
            response.addCookie(cookiePass);
            return "redirect:/toManagerLogin";
        }
    }

    @RequestMapping(value = "/exit")
    public String exit(HttpSession session,HttpServletResponse response) {
        Admin admin = (Admin)session.getAttribute("admin");
        Cookie cookieName = new Cookie("aname",admin.getAname());
        Cookie cookiePass = new Cookie("apass",admin.getApass());
        cookieName.setMaxAge(0);
        cookiePass.setMaxAge(0);
        cookieName.setPath("/");
        cookiePass.setPath("/");
        response.addCookie(cookieName);
        response.addCookie(cookiePass);
        session.invalidate();
        return "redirect:/toManagerLogin";
    }
}
